part of YFree;

///页面基类
abstract class YPage extends State<StatefulWidget> {
  late YPage yPage;
  var yIsAlive = true;
  final Map<String, Function(Map<String, dynamic>? map)> _broadcastReceivers = {};

  @override
  void initState() {
    super.initState();
    yPage = this;
    MethodChannel(runtimeType.toString()).setMethodCallHandler((methodCall) async {
      if (methodCall.method == "onYReceiveBroadcast") {
        var arguments = methodCall.arguments;
        yOnReceiveBroadcast(arguments["broadcastName"], arguments["broadcastParams"]);
      } else {
        return await yOnYFreeCall(methodCall.method, methodCall.arguments);
      }
    });
    yRun(() {
      yOnCreate();
      yOnPostCreate();
    }, delay: 0);
  }

  void yOnCreate();

  void yOnPostCreate() {}

  void yOnDestory() {}

  void yOnReceiveBroadcast(String broadCastName, Map<dynamic, dynamic> arguments) {}

  Future<dynamic> yOnYFreeCall(String method, Map<dynamic, dynamic> arguments) => Future.value(null);

  bool ySafeAreaBottom() => true;

  Widget yBuild(BuildContext context);

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(value: YConfig.statubarTextTheme.copyWith(statusBarColor: YConfig.statusBarColor), child: YCIMGestureDetector(context, Scaffold(backgroundColor: Colors.white, body: SafeArea(top: false, bottom: ySafeAreaBottom(), child: WillPopScope(onWillPop: yIsShowingProgress ? () => Future.value(false) : null, child: yBuild(context))))));
  }

  @override
  void dispose() {
    yFinish(pop: false);
    yOnDestory();
    yIsAlive = false;
    super.dispose();
  }
}

///页面基类(保存状态)
abstract class YKeepAlivePage extends YPage with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return AnnotatedRegion<SystemUiOverlayStyle>(value: YConfig.statubarTextTheme.copyWith(statusBarColor: YConfig.statusBarColor), child: YCIMGestureDetector(context, Scaffold(backgroundColor: Colors.white, body: SafeArea(top: false, bottom: ySafeAreaBottom(), child: WillPopScope(onWillPop: yIsShowingProgress ? () => Future.value(false) : null, child: yBuild(context))))));
  }
}
